/*
 *   Copyright 2008 Mark Ashworth <javameme@gmail.com>.
 * 
 *   Licensed under the GNU General Public License v3 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 * 
 *        http://www.gnu.org/licenses/gpl-3.0.html
 * 
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *   under the License.
 */
package xperiment.metaphor.model.entity;

import org.springframework.security.GrantedAuthority;
import org.springframework.security.userdetails.UserDetails;
import xperiment.metaphor.model.entity.Person;

/**
 *
 * @author Mark P Ashworth <javameme@gmail.com>
 * @version 1.0.0
 * @since 1.0.0
 */
public class UserDetail implements UserDetails {

    private String username;
    private String password;
    private String confirmPassword;
    private GrantedAuthority[] authorities;

    public UserDetail() {
        super();
    }
    
    public UserDetail(String username, String password, GrantedAuthority[] authorities) {
        super();
        this.username = username;
        this.password = password;
        this.authorities = authorities;
    }

    public UserDetail(Person person) {
        super();
        if (person == null) throw new IllegalArgumentException("Person is null");
        if (person.getUser() == null) throw new IllegalArgumentException("Person user role is null");

        this.username = person.getUser().getUsername();
        this.password = person.getUser().getPassword();

        String[] roles = person.roles();
        authorities = new UserGrantedAuthority[roles.length];
        for (int i = 0; i < roles.length; i++) {
            authorities[i] = new UserGrantedAuthority(roles[i]);
        }
    }

    public GrantedAuthority[] getAuthorities() {
        return authorities;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }
    
    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    
    public boolean isAccountNonExpired() {
        return true;
    }

    public boolean isAccountNonLocked() {
        return true;
    }

    public boolean isCredentialsNonExpired() {
        return true;
    }

    public boolean isEnabled() {
        return true;
    }
}
